// pages/user/user.js
var userarr=[];
Page({

    /**
     * 页面的初始数据
     */
    data: {
        navBar: ['作品','关注','粉丝','获赞'],
        userArr:[],
        imgHead:"https://www.304cloud.top:4321"
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        console.log(options.id);
        let userID = wx.getStorageSync('openid')
        this.setData({
          userID:userID
        })
        let that = this;
        this.setData({
            userId:options.id
        })
        wx.request({
          url : "https://www.304cloud.top:4321/getarticles",
          method: "GET",
          data: {
            user_id:options.id
          },
          header: {
            "Content-Type": "application/json"
          },
          success: (res)=> {
            let user = res.data.data;
            for(var i=0;i<user.length;i++){
              if(options.id == user[i].poster_id)
              {
                this.setData({
                  user:user[i]
                })
              }
            }
            wx.setNavigationBarTitle({
              title: this.data.user.user_name+'·主页',
          })
          }
        })
        wx.getSystemInfo({ 
            success: function (res) { 
                that.setData({ 
                    clientHeight: res.windowHeight 
                }); 
            } 
        })
        wx.setNavigationBarTitle({
            title: options.id+'·主页',
        })
        
        this.ifstar();
        this.getTiezi();
        this.getDianzan();
        this.getFollow();
        this.getFans();
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    ifstar:function(){
      wx.request({
        url : "https://www.304cloud.top:4321/ifstar",
        method: "GET",
        data: {
          user_id:this.data.userID,
          poster_id:this.data.userId
        },
        header: {
          "Content-Type": "application/json"
        },
        success: (res)=> {
          console.log(res.data.data);
          if(res.data.data == true){
            this.setData({
              guanzhuTxt:"已关注",
              guanzhuImg:"1"
            })
          }else{
            this.setData({
              guanzhuTxt:"关注",
              guanzhuImg:"0"
            })
          }
        }
      })
    },
    getTiezi:function(e){
        let tzPoemList = [];
        wx.request({
            url : "https://www.304cloud.top:4321/getotherarticles",
            method: "GET",
            data: {
              user_id:this.data.userId
            },
            header: {
              "Content-Type": "application/json"
            },
            success: (res)=> {
              console.log(res.data.data);
              this.setData({
                tzList:res.data.data
              })
              let tzList = res.data.data;
              for(var i=0;i<tzList.length;i++){
                tzPoemList.push(tzList[i].article_txt.replace(/。/g,'\n').replace(/，/g,'\n'))
              }
              userarr[0]=tzList.length;
              this.setData({
                userArr:userarr,
                tzTxt:tzPoemList,
                imgHead:"https://www.304cloud.top:4321"
              })
            }
          })
    },
    getDianzan:function(e){
        wx.request({
            url: 'https://www.304cloud.top:4321/huozan',
            method:"GET",
            data:{
                user_id:this.data.userId
            },
            header:{
              'content-type':'application/json',
            },
            success:(res)=>
            {
              console.log(res.data.data)
              userarr[3]=res.data.data
              this.setData({
                userArr:userarr
              })
            }
        })
    },
    getFans:function(e){
        let fansList = [];
        let fansNumber;
        wx.request({
            url: 'https://www.304cloud.top:4321/fans',
            method:"GET",
            data:{
                user_id:this.data.userId
            },
            header:{
              'content-type':'application/json',
            },
            success:(res)=>
            {
              console.log(res.data.data)
              userarr[2]=res.data.data.length
              this.setData({
                fansList:res.data.data,
                fansHead:"https://www.304cloud.top:4321",
                userArr:userarr
              })
            }
        })
    },
    guanzhu:function(){
        wx.request({
            url : "https://www.304cloud.top:4321/guanzhu",
            method: "POST",
            data: {
              user_id:this.data.userID,
              followed_id:this.data.userId
            },
            header: {
              "Content-Type": "application/json"
            },
            success: (res)=>{
              console.log(res);
              wx.showToast({
                title: '关注成功！',
                icon: 'success',
                duration: 1000
              });
              this.ifstar();
              this.getFans();
            }
            
          })
          
    },
    getFollow:function(){
        wx.request({
            url: 'https://www.304cloud.top:4321/getguanzhu',
            method:"GET",
            data:{
              user_id:this.data.userId
            },
            header:{
              'content-type':'application/json',
            },
            success:(res)=>
            {
              console.log(res.data.data)
              userarr[1]=res.data.data.length
              this.setData({
                guanzhuList:res.data.data,
                userArr:userarr
              })
            }
        })
    },
    swiperchange: function(e) {
        var that = this 
        console.log(e.detail.current) 
        that.setData({
            'currentTab': e.detail.current
        })
    },
    swichNav: function( e ) {
        var that = this;
        if( this.data.currentTab === e.target.dataset.current ) {
            return false;
        } else {
            that.setData( {
                currentTab: e.target.dataset.current
            })
        }
    },
    goToUserDetail:function(e){
      let userId=e.currentTarget.dataset.id;
      if(userId == this.data.userID){
        wx.switchTab({
          url: '/pages/my/my',
        })
      }else{
        wx.navigateTo({
          url: '/pages/user/user?id=' +userId
        })
      }
      console.log(userId)
    },
    goToTieziDetail:function(e){
        let tieziId=e.currentTarget.dataset.id;
        console.log(tieziId)
        wx.navigateTo({
            url: '/pages/tiezi/tiezi?id=' +tieziId
        })
    }

})